import { defineStore } from "pinia";

export const useWsStore = defineStore("ws", {
  state: () => ({
    // WebSocket URL配置
    downloadWsUrl:
      process.env.NODE_ENV === "development"
        ? "ws://192.168.46.79" + ":8070/rtlab/websocket/download"
        : `ws://${window.location.hostname}:8070/rtlab/websocket/download`,
    // 启动算法模型WebSocket URL配置
    startModelWsUrl:
      process.env.NODE_ENV === "development"
        ? "ws://192.168.46.79" + ":8070/rtlab/websocket/experiment"
        : `ws://${window.location.hostname}:8070/rtlab/websocket/experiment`,
    // 其他WebSocket相关状态
    isConnected: false,
    lastMessage: null,
    error: null,
    // 启动算法模型服务状态
    startModelIsConnected: false,
    startModelLastMessage: null,
    startModelError: null,
    startModelState: "disconnected", // disconnected, connecting, connected, error
  }),

  getters: {
    /**
     * 从URL中提取IP地址
     */
    getIpFromUrl: (state) => {
      try {
        // 解析URL获取主机名（IP地址）
        const url = new URL(state.downloadWsUrl);
        return url.hostname;
      } catch (error) {
        console.error("URL解析错误:", error);
        // 如果URL解析失败，尝试从字符串中提取IP
        const ipMatch = state.downloadWsUrl.match(/(\d+\.\d+\.\d+\.\d+)/);
        return ipMatch ? ipMatch[1] : "localhost";
      }
    },

    /**
     * 获取服务器URL
     */
    getServerUrl: (state) => {
      const ip = state.getIpFromUrl;
      return `${ip}:8070/rtlab`;
    },

    /**
     * 从启动模型URL中提取IP地址
     */
    getIpFromStartModelUrl: (state) => {
      try {
        // 解析URL获取主机名（IP地址）
        const url = new URL(state.startModelWsUrl);
        return url.hostname;
      } catch (error) {
        console.error("URL解析错误:", error);
        // 如果URL解析失败，尝试从字符串中提取IP
        const ipMatch = state.startModelWsUrl.match(/(\d+\.\d+\.\d+\.\d+)/);
        return ipMatch ? ipMatch[1] : "localhost";
      }
    },

    /**
     * 获取启动模型服务器URL
     */
    getStartModelServerUrl: (state) => {
      const ip = state.getIpFromStartModelUrl;
      return `${ip}:8070/rtlab`;
    },
  },

  actions: {
    /**
     * 设置WebSocket URL
     */
    setWsUrl(url) {
      this.downloadWsUrl = url;
    },

    /**
     * 更新连接状态
     */
    setConnectionStatus(status) {
      this.isConnected = status;
    },

    /**
     * 记录最后收到的消息
     */
    setLastMessage(message) {
      this.lastMessage = message;
    },

    /**
     * 设置错误信息
     */
    setError(error) {
      this.error = error;
    },

    /**
     * 设置启动模型WebSocket URL
     */
    setStartModelWsUrl(url) {
      this.startModelWsUrl = url;
    },

    /**
     * 更新启动模型连接状态
     */
    setStartModelConnectionStatus(status) {
      this.startModelIsConnected = status;
    },

    /**
     * 记录启动模型最后收到的消息
     */
    setStartModelLastMessage(message) {
      this.startModelLastMessage = message;
    },

    /**
     * 设置启动模型错误信息
     */
    setStartModelError(error) {
      this.startModelError = error;
    },

    /**
     * 设置启动模型状态
     */
    setStartModelState(state) {
      this.startModelState = state;
    },
  },
});
